package com.witframework.components.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.PropertiesConstants;
import com.witframework.WitException;
import com.witframework.core.ApplicationProperties;

/**
 * @author wuxinyang
 * Jun 4, 2009 9:37:20 AM
 * 
 */
public class DataSetAuthSelectTag extends TagSupport{
	private String id;//选择组件的ID
	private String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	private String properties;//在列表中显示的字段，对于PAGE类型则忽略该属性
	private String datatype="class";
	private String titles;//列表的字段标题,如："name|实体名称,comment|说明,catalog|分类"，对于PAGE类型则忽略该属性
	private String multiple;//是否允许多选，对于PAGE类型则忽略该属性
	private String selected;//尚未完全实现，默认的选中项
	private String property1;
	private String property2;
	private String property3;
	private String filterOptions;//可设定的过滤项,目前只支持字符串类型的属性,如:"catalog,name"，对于PAGE类型则忽略该属性
	private String width;//对话框宽度
	private String height;//对话框高度	
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		JspWriter writer = pageContext.getOut();
		
		if((datatype==null||datatype.equalsIgnoreCase("table")||datatype.equalsIgnoreCase("sql"))
			&&(properties==null)){
			throw new WitException("非page类型的数据源时,必须设置properties属性");
		}
		StringBuffer dataUrl=new StringBuffer("/dataSetAuthSelectController"+ApplicationProperties.getProperty(PropertiesConstants.WIT));//数据列表的URL
		if(StringUtils.isNotEmpty(datatype)&&datatype.equalsIgnoreCase("page")){
			dataUrl=new StringBuffer(data);
		}		
		if(dataUrl.toString().startsWith("/")&&!dataUrl.toString().startsWith(request.getContextPath())) dataUrl=new StringBuffer(request.getContextPath()+dataUrl);
		dataUrl.append((dataUrl.indexOf("?")>0?"&":"?")+"dialogid="+id+"&data="+getData());	
		if(StringUtils.isNotEmpty(datatype)||!datatype.equalsIgnoreCase("page")){
			if(StringUtils.isNotEmpty(properties)) dataUrl.append("&properties="+properties);	
			if(StringUtils.isNotEmpty(titles))	dataUrl.append("&titles="+getTitles());		
			if(StringUtils.isNotEmpty(selected))	dataUrl.append("&selected="+selected);		
			if(StringUtils.isNotEmpty(datatype)) dataUrl.append("&datatype="+datatype);
			if(StringUtils.isNotEmpty(property1)) dataUrl.append("&property1="+property1);
			if(StringUtils.isNotEmpty(property2)) dataUrl.append("&property2="+property2);
			if(StringUtils.isNotEmpty(property3)) dataUrl.append("&property3="+property3);
			if(StringUtils.isNotEmpty(filterOptions))	dataUrl.append("&filterOptions="+filterOptions);	
		}
		if(StringUtils.isNotEmpty(multiple))	dataUrl.append("&multiple="+multiple);
		
		StringBuffer html=new StringBuffer("<iframe");		
		html.append(" src=\""+dataUrl+"\" width='"+width+"' frameborder='0' height='"+height+"'></iframe>");	
		try {			
			writer.println(html);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}

	/**
	 * @param data
	 */
	public void setData(String data) {			
		this.data = data;
		try {
			this.data=(String) ExpressionEvaluatorManager.evaluate("data",data,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setId(String id) {
		this.id = id;
		try {
			this.id=(String) ExpressionEvaluatorManager.evaluate("id",id,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param properties
	 */
	public void setProperties(String properties) {
		this.properties = properties;
		try {
			this.properties=(String) ExpressionEvaluatorManager.evaluate("properties",properties,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}	

	public void setDatatype(String datatype) {
		this.datatype = datatype;
		try {
			this.datatype=(String) ExpressionEvaluatorManager.evaluate("datatype",datatype,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setTitles(String titles) {
		this.titles = titles;
		try {
			this.titles=(String) ExpressionEvaluatorManager.evaluate("titles",titles,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
		try {
			this.multiple=(String) ExpressionEvaluatorManager.evaluate("multiple",multiple,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setSelected(String selected) {
		this.selected = selected;		
		try {
			this.selected=(String) ExpressionEvaluatorManager.evaluate("selected",selected,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}	

	public String getId() {
		return id;
		
		
	}

	public String getData() {
		try {
			return java.net.URLEncoder.encode(data,"utf-8");
		} catch (Exception e1) {			
		}
		return data;
	}

	public String getProperties() {
		return properties;
	}


	public String getDatatype() {
		return datatype;
	}

	public String getTitles() {
		try {
			return java.net.URLEncoder.encode(titles,"utf-8");
		} catch (Exception e1) {			
		}
		return titles;
	}

	public String getMultiple() {
		return multiple;
	}

	public String getSelected() {
		return selected;
	}

	public void setProperty1(String property1) {
		this.property1 = property1;
		try {
			this.multiple=(String) ExpressionEvaluatorManager.evaluate("multiple",multiple,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setProperty2(String property2) {
		this.property2 = property2;
		try {
			this.property2=(String) ExpressionEvaluatorManager.evaluate("property2",property2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setProperty3(String property3) {
		this.property3 = property3;
		try {
			this.property3=(String) ExpressionEvaluatorManager.evaluate("property3",property3,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setFilterOptions(String filterOptions) {
		this.filterOptions = filterOptions;
		try {
			this.filterOptions=(String) ExpressionEvaluatorManager.evaluate("filterOptions",filterOptions,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setWidth(String width) {
		this.width = width;
		try {
			this.width=(String) ExpressionEvaluatorManager.evaluate("width",width,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		
	}
	public void setHeight(String height) {
		this.height = height;
		try {
			this.height=(String) ExpressionEvaluatorManager.evaluate("height",height,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
}
